set more off
clear all

global BHHDIR Dropbox\INE\data\bhh
global ENIADIR Dropbox\INE\data\matchip
global INNODIR Dropbox\INE\data\Innovacion

/*
global BHHDIR C:\Users\chelmers\Dropbox\INE\data\bhh
global ENIADIR C:\Users\chelmers\Dropbox\INE\data\matchip
global INNODIR C:\Users\chelmers\Dropbox\INE\data\Innovacion
*/

cap log close
log using $BHHDIR\cr_sample_clean,t replace

*** Program to create sample for DiD and other estimation 
use $BHHDIR/sample, clear
sort id year
drop tfp* soc_* coop_* bar_* ef_* *_inno

*** Get the tfp estimates
merge 1:1 id year using $BHHDIR\tfp_ind
drop if _m==1
drop _m

*** Restrict to technology-intensive sectors only
*keep if (ind>22 & ind<28) | ind==33 | ind==34

*** Additional cleaning and variable construction

*drop if foreign
*drop if abs(lag)>6

*** Recode ind to increase sample size in each sector
gen indold = ind
replace ind = 18 if ind==19
replace ind = 20 if ind==21
replace ind = 28 if ind==27
replace ind = 31 if ind==33
replace ind = 34 if ind==35

label val indold ilbl
label def i2lbl 15 "food products and beverages, tobacco" 
label def i2lbl 17 "textiles", add
label def i2lbl 18 "wearing apparel & leather goods", add
label def i2lbl 20 "wood, paper, cork and straw products", add
label def i2lbl 22 "publishing, printing and reproduction of recorded media", add
label def i2lbl 24 "chemicals and chemical products incl coke & refined oil", add
label def i2lbl 25 "rubber and plastics products", add
label def i2lbl 26 "other non-metallic mineral products", add
label def i2lbl 28 "basic & fabricated metal products", add
label def i2lbl 29 "machinery and equipment n.e.c.", add
label def i2lbl 31 "elec & comp machinery, instruments", add
label def i2lbl 34 "motor vehicles & other transport equipment", add
label def i2lbl 36 "furniture; manufacturing n.e.c.", add
label def i2lbl 37 "recycling", add
label val ind i2lbl
label var ind "Reduced 2-digit ISIC classes"

keep id year firstyr* pat* tm* log* dlog* tfp* dtfp* dip dtm dpat doth dum ind ///
     foreign mixed public indiv export santiago hhi share4 shfor indold

label var tfp_acf "ACF estimate of firm TFP for agg sample"
label var tfp_acf_i "ACF estimate of firm TFP by 2-digit industry"	 

do $BHHDIR\cleanfile
sum
save $BHHDIR\sample_clean, replace

log close
